<?php
namespace Tyrone\Model;
use Medoo\Medoo;

class DayOnline extends Base
{
    protected $table = 'c_day_online';


    public function insertData($item,$minus,$field,$other)
    {
        //写入或更新当前在线数据
        $uniqueKey = ["day", "type", "platform", "plat_server", "stat_project"];
        $uniqueData = array_intersect_key($item,array_flip($uniqueKey));
        $checkInfo = $this->database->get($this->table,['id','online_txt'],$uniqueData);
        $num = $item[$field];
        unset($item[$field]);
        $add = $uniqueData + $other;
        //修改逻辑
        if ($checkInfo['id'] > 0) {
            $add['online_txt'] = unserialize($checkInfo['online_txt']);
            $add['online_txt'][$minus] = (int)$num;//加入当前时间的在线人数
            $add['max_num'] = max($add['online_txt']);
            $add['min_num'] = min($add['online_txt']);
            $add['avg_num'] = (int)(array_sum($add['online_txt']) / count($add['online_txt']));
            $this->database->update($this->table,$add,$uniqueData);
            $add['id'] = $checkInfo['id'];
        } //添加
        else {

            $add['online_txt'][$minus] = (int)$num;//加入当前时间的在线人数
            $add['min_num'] = $add['max_num'] = $add['avg_num'] = (int)$num;
            $this->database->insert($this->table,$add);
            $add['id'] = $this->database->id();
        }
        return $add;
    }

}